﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.IO;


namespace XmlProcessor
{
    public class XmlValidator
    {
        private bool status;
        /// <summary>
        /// Validates a XML against an schema an present the results in
        /// the console.
        /// </summary>
        /// <param name="schema"> Path to the XML schema. File with xsd extension</param>
        /// <param name="xml">Path to the XML file.</param>
        public XmlValidator(String schema, String xml)
        {
            this.status = true;
            // Create the XmlSchemaSet class.
            XmlSchemaSet sc = new XmlSchemaSet();

            // Add the schema to the collection.
            sc.Add(null, schema);

            // Set the validation settings.
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas = sc;
            settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

            // Create the XmlReader object.
            XmlReader reader = XmlReader.Create(xml, settings);

            // Parse the file. 
            while (reader.Read()) ;

        }
        public bool getStatus()
        {
            return this.status;
        }
        // Display any validation errors.
        private void ValidationCallBack(object sender, ValidationEventArgs e) {
            this.status = false;
            Console.WriteLine("Validation Error: {0}", e.Message);
        }

    }
}
